package com.xbh.management.job;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xbh.common.utils.JwtUtil;
import com.xbh.management.enums.AttendanceTypeEnum;
import com.xbh.management.model.ManAttendance;
import com.xbh.management.model.ManLeave;
import com.xbh.management.model.ManUser;
import com.xbh.management.service.AttendanceService;
import com.xbh.management.service.ManLeaveService;
import com.xbh.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-14 10:26
 */

@Component
@Slf4j
public class TaskScheduledAttendance {

    @Autowired
    private UserService userService;

    @Autowired
    private AttendanceService attendanceService;

    @Autowired
    private ManLeaveService manLeaveService;


    @Scheduled(cron = "0 55 23 1/1 * ?")
    private void taskScheduledAttendance(){

        log.info("【查询用户是否打卡】定时任务开始执行");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(new Date());
        String subTime = startTime.substring(0, 10);

        String start = "00:00:01";
        String end = "23:59:59";
        //一天开始时间
        String startData = subTime+ " " + start;
        //一天结束时间
        String endData = subTime+ " " + end;

        LambdaQueryWrapper<ManUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(ManUser::getDeleted,new Integer(0));
        List<ManUser> userList = userService.list(userWrapper);

        userList.stream().forEach(user ->{
            LambdaQueryWrapper<ManAttendance> attendanceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            try {
                attendanceLambdaQueryWrapper.eq(ManAttendance::getUserId, user.getUserId());
                attendanceLambdaQueryWrapper.and(
                        LambdaQueryWrapper -> LambdaQueryWrapper.eq(ManAttendance::getAttendanceType, AttendanceTypeEnum.PUNCH_CARD.getCode())
                        .or()
                        .eq(ManAttendance::getAttendanceType,AttendanceTypeEnum.LATE_PUNCH_CARD.getCode())
                        .or()
                        .eq(ManAttendance::getAttendanceType,AttendanceTypeEnum.NO_PUNCH_CARD.getCode())
                        .or()
                        .eq(ManAttendance::getAttendanceType,AttendanceTypeEnum.LEAVE.getCode())
                );
                attendanceLambdaQueryWrapper.between(ManAttendance::getCreateTime,simpleDateFormat.parse(startData),simpleDateFormat.parse(endData));
            } catch (ParseException e) {
                throw new RuntimeException("时间格式转换异常");
            }
            List<ManAttendance> attendanceList = attendanceService.list(attendanceLambdaQueryWrapper);
            //当条用户当天没有上班打卡
            if(CollectionUtil.isEmpty(attendanceList)){
                log.info(subTime+"-------"+user.getPersonName()+"-----"+ "没有完成上班打卡");
                ManAttendance manAttendance = new ManAttendance();
                manAttendance.setUserId(user.getUserId());
                manAttendance.setUsername(user.getPersonName());
//                manAttendance.setStartTime(new Date());
                manAttendance.setAttendanceType(AttendanceTypeEnum.NO_PUNCH_CARD.getCode());
                manAttendance.setCreatePerson("admin");
                manAttendance.setUpdatePerson("admin");
                //判断当前用户是否请假
                LambdaQueryWrapper<ManLeave> manLeaveLambdaQueryWrapper = new LambdaQueryWrapper<>();

                manLeaveLambdaQueryWrapper.eq(ManLeave::getUserId, user.getUserId());
                try {
                    manLeaveLambdaQueryWrapper.between(ManLeave::getCreateTime,simpleDateFormat.parse(startData),simpleDateFormat.parse(endData));
                } catch (ParseException e) {
                    throw new RuntimeException("时间格式转换异常");
                }
                List<ManLeave> leaveList = manLeaveService.list(manLeaveLambdaQueryWrapper);
                if(CollectionUtil.isNotEmpty(leaveList)){
                    manAttendance.setAttendanceType(AttendanceTypeEnum.LEAVE.getCode());
                }
                attendanceService.save(manAttendance);
            }
        });
        log.info("【查询用户是否打卡】定时任务执行结束");
    }
}
